package middleware

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"user/pkg"
)

func JwtAuthMiddleware() gin.HandlerFunc {
	return func(c *gin.Context) {
		token := c.Request.Header.Get("token")
		if token == "" {
			c.AbortWithStatusJSON(http.StatusOK, gin.H{
				"Code": -1,
				"Msg":  "token为空",
			})
			return
		}
		parseToken, err := pkg.ParseToken(token)
		if err != nil {
			c.AbortWithStatusJSON(http.StatusOK, gin.H{
				"Code": -1,
				"MSg":  "token解析失败",
			})
			return
		}
		c.Set("userId", parseToken)
	}
}
